import pymysql
class connect:
    def __init__(self, host, user, passwd, port=3306):
        self.host = host
        self.port = port
        self.user = user
        self.passwd = passwd
    def get_db(self):
        """
        获取数据库列表
        :return: 返回数据库列表
        """
        try:
            connects = pymysql.connect(user=self.user, passwd=self.passwd, host=self.host, port=self.port)
            cmd = connects.cursor()
        except:
            print('数据库连接失败')
            return False
        cmd.execute("show databases;")
        get_dbs = cmd.fetchall()
        cmd.close()
        if get_dbs:
            db = []
            for i in get_dbs:
                db.append(i[0])
            return db
        else:
            return False
    def get_tables(self,db):
        """
        获取数据表
        :param db: 数据库名称
        :return: 数据表列表
        """
        try:
            connects = pymysql.connect(user=self.user, passwd=self.passwd, host=self.host, port=self.port)
            cmd = connects.cursor()
        except:
            print('数据库连接失败')
            return False
        cmd.execute(f"use {db};")
        cmd.execute(f"show tables;")
        get_tables = cmd.fetchall()
        cmd.close()
        if get_tables:
            table = []
            for i in get_tables:
                table.append(i[0])
            return table
        else:
            return False



get = connect(user='step', host='localhost', port=3306, passwd='passwd?')
db_list = get.get_db()

table_list = get.get_tables(db='Steps_today')


old_get = connect(user='step', host='localhost', port=3306, passwd='passwd?')
old_db_list = old_get.get_db()
if len(db_list) == len(old_db_list):
    print(f"新旧服务器数据库数量一致: {len(db_list)} 个")
    print(f"正在以旧服务器数据库对比新服务器数据库")
    for i in db_list:
        if i in old_db_list:
            print(f"数据库: [ {i} ] 已匹配")
            old_table = old_get.get_tables(db=str(i))
            new_table = get.get_tables(db=str(i))
            if len(old_table) == len(new_table):
                print(f"[ {i} ] 数据表数量已匹配,数量: [ {len(new_table)} ] 个")
            else:
                print(f"[ {i} ] 数据表数量不匹配")
        else:
            print(f"[ {i} ] 未匹配，在新服务器中找不到该数据库")
else:
    print(f"新旧数据库数量不一致"
          f"新服务器数据库: [ {len(db_list)} ] 个"
          f"旧服务器数据库: [ {len(old_db_list)} ]")